package edu.ccut.saturn.studio.saturndata.page;

import java.util.ArrayList;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.events.ExpansionAdapter;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;


import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.saturndata.editor.SaturnDataEditor;
import edu.ccut.saturn.studio.saturndata.model.SaturnData;
import edu.ccut.saturn.studio.saturndata.model.SaturnDataModel;

/**
 * 概述页
 * 
 * @author 谷钰
 * 
 * @version 1.0 2008.12.09
 * 
 */
public class OverViewPage extends FormPage {

	private Text nameText;
	private Text spacenameText;
	private Text descriptionText;

	private TableViewer fieldTableViewer;

	private SaturnDataModel saturnDataModel;
	private SaturnData saturnData;

	public static final String Page_ID = "freepage"; //$NON-NLS-1$

	public OverViewPage(SaturnDataEditor editor) {
		super(editor, "first", Messages.getString("OverViewFormPage.summarize")); //$NON-NLS-1$ //$NON-NLS-2$

		this.saturnDataModel = editor.getSaturnDataModel();
		this.saturnData = saturnDataModel.getSaturnData();
	}

	protected void createFormContent(IManagedForm managedform) {
		ScrolledForm scrolledform = managedform.getForm();
		FormToolkit toolkit = managedform.getToolkit();
		scrolledform.setText(Messages.getString("OverViewFormPage.summarize")); //$NON-NLS-1$
		GridLayout gridlayout = new GridLayout();
		gridlayout.numColumns = 2;
		// gridlayout.horizontalSpacing = 20;
		scrolledform.getBody().setLayout(gridlayout);

		this.createEntityComposite(scrolledform, toolkit);
		this.createFieldInfoComposite(scrolledform, toolkit, saturnData);
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	private void createEntityComposite(final ScrolledForm form,
			FormToolkit formtoolkit) {

		Section section = formtoolkit.createSection(form.getBody(), 384);
		GridData gridDataC = new GridData(SWT.FILL, SWT.FILL, true, true);
		section.setLayoutData(gridDataC);

		Composite composite = new Composite(section, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);

		Label labelname = formtoolkit.createLabel(composite, Messages
				.getString("OverViewFormPage.name")); //$NON-NLS-1$
		labelname.setForeground(formtoolkit.getColors().getColor(
				"org.eclipse.ui.forms.TITLE")); //$NON-NLS-1$

		nameText = new Text(composite, SWT.BORDER | SWT.READ_ONLY); //$NON-NLS-1$
		GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		nameText.setLayoutData(gd_nameText);
		nameText.setText(saturnData.getType());
		new Label(composite, SWT.NULL);
		new Label(composite, SWT.NULL);

		Label label1space = formtoolkit.createLabel(composite, Messages
				.getString("OverViewFormPage.namespace")); //$NON-NLS-1$
		label1space.setLayoutData(new GridData());
		label1space.setForeground(formtoolkit.getColors().getColor(
				"org.eclipse.ui.forms.TITLE")); //$NON-NLS-1$
		GridData text_data = new GridData(SWT.FILL, SWT.CENTER, true, false);

		spacenameText = new Text(composite, SWT.BORDER | SWT.READ_ONLY); //$NON-NLS-1$
		spacenameText.setLayoutData(text_data);
		spacenameText.setText(saturnData.getSpace());
		new Label(composite, SWT.NULL);
		new Label(composite, SWT.NULL);
		Label labeldescription = formtoolkit.createLabel(composite, Messages
				.getString("OverViewFormPage.desciption")); //$NON-NLS-1$
		labeldescription.setLayoutData(new GridData());
		labeldescription.setForeground(formtoolkit.getColors().getColor(
				"org.eclipse.ui.forms.TITLE")); //$NON-NLS-1$

		descriptionText = new Text(composite, SWT.V_SCROLL | SWT.BORDER
				| SWT.H_SCROLL);
		GridData textdescription = new GridData(SWT.FILL, SWT.FILL, true, true);
		textdescription.horizontalSpan = 2;
		descriptionText.setLayoutData(textdescription);
		descriptionText.setText(saturnData.getDescription());
		formtoolkit.adapt(descriptionText, true, true);
		descriptionText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent modifyevent) {
				String description = descriptionText.getText().trim();

				if (!description.equals(saturnData.getDescription())) {
					saturnData.setDescription(description);
					saturnDataModel.changeDataNodeValue(
							SaturnDataModel.DATA_ELEMENT_DESCRIPTION,
							description);

					SaturnProjectUtil.refreshProject(OverViewPage.this
							.getEditorInput());
				}
			}
		});
		section.setText(Messages.getString("OverViewFormPage.summarizemessage")); //$NON-NLS-1$
		adaptRecursively(formtoolkit, composite);
		section.setClient(composite);
		section.setExpanded(true);
		section.addExpansionListener(new ExpansionAdapter() {
			public void expansionStateChanged(ExpansionEvent expansionevent) {
				form.reflow(false);
			}
		});
	}

	private void createFieldInfoComposite(final ScrolledForm scrolledform,
			FormToolkit formtoolkit, final SaturnData dataVo) {

		Section section = formtoolkit
				.createSection(scrolledform.getBody(), 400);

		GridData gridDataC = new GridData(SWT.FILL, SWT.FILL, true, true);
		section.setLayoutData(gridDataC);

		Composite composite = new Composite(section, SWT.NULL);
		final GridLayout gridLayout = new GridLayout();
		composite.setLayout(gridLayout);

		Table table = formtoolkit.createTable(composite, 68098);

		table.setLayout(new GridLayout());
		GridData griddataT = new GridData();
		griddataT.heightHint = 100;
		table.setLayoutData(griddataT);
		formtoolkit.paintBordersFor(table);

		fieldTableViewer = new TableViewer(table);

		fieldTableViewer
				.setContentProvider(new SaturnSWTUtils.SaturnTableViewerDefaultContentProvider());
		fieldTableViewer
				.setLabelProvider(new SaturnSWTUtils.SaturnTableViewerDefaultLabelProvider() {
					public String getColumnText(Object element, int columnIndex) {
						IDataItem dataItem = (IDataItem) element;
						if (columnIndex == 0) {
							return dataItem.getName();
						}
						if (columnIndex == 1) {
							return dataItem.getColumn();
						}
						if (columnIndex == 2) {
							return dataItem.getJavaType();
						}
						if (columnIndex == 3) {
							return dataItem.getType();
						}
						if (columnIndex == 4) {
							return dataItem.getTableName();
						}

						return Messages.getString("OverViewPage.0"); //$NON-NLS-1$
					}
				});

		GridData gridData1 = new GridData(SWT.FILL, SWT.FILL, true, true);
		gridData1.horizontalIndent = 0;
		gridData1.heightHint = 320;
		gridData1.widthHint = 590;
		table.setLayoutData(gridData1);
		table.setHeaderVisible(true); // 显示表头
		table.setLinesVisible(true); // 显示表格线

		TableColumn column0 = new TableColumn(table, SWT.NULL);
		column0.setText(Messages.getString("OverViewFormPage.fieldproperty")); //$NON-NLS-1$
		column0.setWidth(62);
		TableColumn column1 = new TableColumn(table, SWT.NULL);
		column1.setText(Messages.getString("OverViewFormPage.columnname")); //$NON-NLS-1$
		column1.setWidth(45);
		TableColumn column2 = new TableColumn(table, SWT.NULL);
		column2.setText(Messages.getString("OverViewFormPage.javatype")); //$NON-NLS-1$
		column2.setWidth(64);

		TableColumn column3 = new TableColumn(table, SWT.NULL);
		column3.setText(Messages.getString("OverViewFormPage.databasetype")); //$NON-NLS-1$
		column3.setWidth(68);

		TableColumn column4 = new TableColumn(table, SWT.NULL);
		column4.setText(Messages.getString("OverViewFormPage.tablename")); //$NON-NLS-1$
		column4.setWidth(63);
		fieldTableViewer.setInput(new ArrayList<IDataItem>(dataVo.values()));

		GridData griddata1 = new GridData(1808);
		table.setLayoutData(griddata1);

		section.setText(Messages.getString("OverViewFormPage.fieldproperty")); //$NON-NLS-1$
		section.setDescription(Messages.getString("OverViewFormPage.operationforfieldproperty")); //$NON-NLS-1$

		adaptRecursively(formtoolkit, composite);
		section.setClient(composite);
		section.setExpanded(true);
		section.addExpansionListener(new ExpansionAdapter() {
			public void expansionStateChanged(ExpansionEvent expansionevent) {
				scrolledform.reflow(false);
			}
		});
	}

	public void onChangePage(SaturnDataEditor editor) {
		this.saturnDataModel = editor.getSaturnDataModel();
		this.saturnData = saturnDataModel.getSaturnData();

		if (this.descriptionText != null) {
			this.descriptionText.setText(saturnData.getDescription());
		}

		if (fieldTableViewer != null) {
			fieldTableViewer.setInput(saturnData.getDataItemList());
		}
	}

	protected void adaptRecursively(FormToolkit tk, Composite composite) {
		tk.adapt(composite);
		Control[] children = composite.getChildren();
		for (int i = 0; i < children.length; i++) {
			Control control = children[i];
			if (control instanceof Composite) {
				adaptRecursively(tk, (Composite) control);
			} else {
				tk.adapt(control, true, false);
			}
		}
	}
}